/*基于GPLv2发布
2007/12/16
作者：旅行者2号
laf163@gmail.com*/

#include <MainDialog.h>
#include <QtGui>

extern QList <struct struct_arplist>arplist;

MainDialog::MainDialog(QWidget *parent)
	:QDialog(parent)
{
	ui.setupUi(this);
	Qt::WindowFlags flags=0;
	flags |= Qt::MSWindowsFixedSizeDialogHint;
	flags |= Qt::WindowSystemMenuHint;
	flags |= Qt::WindowTitleHint;
	flags |= Qt::WindowMinimizeButtonHint;
	flags |= Qt::WindowShadeButtonHint;
	this->setWindowFlags(flags);
	this->setFont(QApplication::font());
	ui.tableWidget->setFont(QApplication::font());
	pMenuBar=new QMenuBar(this);
	pMenuBar->setFont(QApplication::font());
	pMenuBar->setGeometry(0,0,400,50);

	QMenu *pMenuOption=pMenuBar->addMenu(tr("选项(&O)"));
	QMenu *pMenu=pMenuBar->addMenu(tr("帮助(&H)"));

	QAction *pAction_aboutqt=pMenu->addAction(tr("关于QT"));
	QAction *pAction_about=pMenu->addAction(tr("关于ShadaArp"));

	QAction *pAction_set=pMenuOption->addAction(tr("设置字体(&S)"));

	connect(pAction_aboutqt, SIGNAL(triggered()), this, SLOT(aboutQt()));
	connect(pAction_about, SIGNAL(triggered()), this, SLOT(about()));
	connect(pAction_set, SIGNAL(triggered()), this, SLOT(option_set()));

	ui.tableWidget->setRowCount(10);
	printf("Starting CaptureThread ...");
	pcapthrd=new CaptureThread();
	pcapthrd->start();
	printf("[ok].\n");
	//startTimer(3000);
}

void MainDialog::aboutQt()
{
	QMessageBox::aboutQt(this);
}

void MainDialog::about()
{
	QMessageBox::information(0,tr("关于ShadaArp"),tr("版本：1.0alpha2\n作者：土星五号 \nQQ群：24032501\nlaf163@gmail.com"));
}

void MainDialog::option_set()
{
	bool ok;
 	QFont font = QFontDialog::getFont(&ok, this->font(),this);
 	if (ok) {
		QSettings settings("ShadaLinux", "ShadaArp");
		settings.setValue("appfont",font.family());
		settings.setValue("appfontsize",font.pointSize());
		settings.sync();
		this->setFont(font);
		ui.tableWidget->setFont(font);
		pMenuBar->setFont(font);
	}
	else {
		// the user canceled the dialog; font is set to the default
		// application font, QApplication::font()
	}
}

void MainDialog::refresh()
{
	ui.tableWidget->clearContents();
	ui.tableWidget->setRowCount(arplist.count());
	int i;
	for(i=0;i<arplist.count();i++){
		struct_arplist tmparplist;
		tmparplist=arplist.at(i);
		//printf("debug:op=%d\n",tmparplist.op);
		//printf("debug:%s\n",tmparplist.sip);
		ui.tableWidget->setItem(i,0,new QTableWidgetItem(tmparplist.sip));
		ui.tableWidget->setItem(i,1,new QTableWidgetItem(tmparplist.sha));
		ui.tableWidget->setItem(i,2,new QTableWidgetItem(tmparplist.tip));
		ui.tableWidget->setItem(i,3,new QTableWidgetItem(tmparplist.tha));
		ui.tableWidget->setItem(i,4,new QTableWidgetItem(tmparplist.op==1?tr("请求"):tr("应答")));
		ui.tableWidget->setItem(i,5,new QTableWidgetItem(QString::number(tmparplist.count,10)));
	}
	ui.tableWidget->resizeColumnsToContents();
	ui.tableWidget->resizeRowsToContents();
	ui.tableWidget->update();
}

void MainDialog::timerEvent(QTimerEvent *event)
{
	//ui.tableWidget->update();
}

void MainDialog::customEvent(QEvent * event)
{
	refresh();
}

